package com.huobi.event;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobi.db.bean.HbLineData;
import com.huobi.db.bean.HbTradeDetail;

@Component
public class EventPublisher { 

	@Autowired ApplicationEventPublisher publisher;
	
	/**
	 * K线行情
	 * @param data
	 */
	@Async
	public void publishKLine(JSONObject data) {
		final JSONObject tick = data.getJSONObject("tick");
		if (tick == null) {
			return;
		}
		Date tradeDate = data.getDate("ts");
		HbLineData line = convertToLineDetail(tick);
		line.setTradeDate(tradeDate);
		publisher.publishEvent(new LineDataEvent(new HbLineData[] {line}));
	}


	/**
	 * 创建事件
	 * 
	 * @param source
	 * @param userId 业务数据
	 */
	@Async
	public void publishTrade(JSONObject data) {
		final String tradeType = data.getString("ch"); 
		final JSONArray jsonArray = data.getJSONObject("tick").getJSONArray("data");
		final int size = jsonArray.size();
		if (size <= 0)
			return;
		final List<HbTradeDetail> lists = new ArrayList<>();
		IntStream.range(0, size).forEach(idx -> {
			JSONObject obj = jsonArray.getJSONObject(idx);
			HbTradeDetail detail = convertToTradeDetail(obj);
			detail.setTradeType(tradeType);
			lists.add(detail);
		});
		if (lists.size() > 0) {
			publisher.publishEvent(new TradeDataEvent(lists.stream().toArray(HbTradeDetail[]::new)));
		}
	}

	private HbLineData convertToLineDetail(JSONObject tick) {
		BigDecimal high = tick.getBigDecimal("high");
		Long amount = tick.getLong("amount");
		Long vol = tick.getLong("vol");
		BigDecimal trade_turnover = tick.getBigDecimal("trade_turnover");
		BigDecimal low = tick.getBigDecimal("low");
		BigDecimal close= tick.getBigDecimal("close");
		BigDecimal open = tick.getBigDecimal("open");
		HbLineData data = new HbLineData();
		data.setAmount(amount);
		data.setClose(close);
		data.setHigh(high);
		data.setLow(low);
		data.setOpen(open);
		data.setTradeTurnover(trade_turnover);
		data.setVol(vol);
		return data;
	}
	
	private HbTradeDetail convertToTradeDetail(JSONObject obj) {
		Long id = obj.getLong("id");
		BigDecimal amount = obj.getBigDecimal("amount");
		BigDecimal quantity = obj.getBigDecimal("quantity");
		BigDecimal price = obj.getBigDecimal("price");
		BigDecimal trade_turnover = obj.getBigDecimal("trade_turnover");
		Date tradeDate = obj.getDate("ts");
		String direction = obj.getString("direction");
		final HbTradeDetail tradeDetail = new HbTradeDetail();
		tradeDetail.setTradeId(id.toString());
		tradeDetail.setAmount(amount);
		tradeDetail.setDirection(direction);
		tradeDetail.setPrice(price);
		tradeDetail.setQuantity(quantity);
		tradeDetail.setTradeTurnover(trade_turnover);
		tradeDetail.setTradeDate(tradeDate);
		return tradeDetail;
	}

}
